package com.wanmait.zaisousuo.service.Impl;

import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.TextureService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.wanmait.zaisousuo.mapper.TextureMapper;
import com.wanmait.zaisousuo.pojo.Texture;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TextureServiceImpl implements TextureService {

    @Resource
    private TextureMapper textureMapper;


    @Override
    public void insert(Texture texture ) {
       textureMapper.insert(texture);
    }

    //更新材质信息
    @Override
    public void update(Texture texture) {
         textureMapper.updateByPrimaryKeySelective(texture);
    }

    //真删除垃圾材质
    @Override
    public void delete(Integer id) {
        if(textureMapper.findChildren(id).size()==0){
            textureMapper.deleteByPrimaryKey(id);
        }
        else
        {
            textureMapper.deleteAll(textureMapper.findChildren(id),false);
            textureMapper.deleteByPrimaryKey(id);
        }


    }

    @Override
    public void delete(String ids) {

    }

    //根据材质id查询
    @Cacheable(value = "TextureCache",key = "'findById'+#id")
    @Override
    public Texture findById(Integer id) {
        return textureMapper.selectByPrimaryKey(id);
    }

    //查询出所有的父级材质
    @Cacheable(value = "TextureCache",key = "'findAll'")
    @Override
    public List<Texture> findAll() {
        return textureMapper.findAll();
    }

    @Override
    public List<Texture> findAll(Texture object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(Texture object) {
        return 0;
    }

    @Override
    public PageInfo<Texture> findList(int pageNum) {
        return null;
    }

    //根据父类材质id查询出所有子类材质
    @Cacheable(value = "TextureCache",key = "'findChildrenTexture'+#textId")
    @Override
    public List<Texture> findChildrenTexture(Integer textId) {
        return textureMapper.findChildrenTexture(textId);
    }

    @Override
    public List<Texture> listAllTexture(Integer state) {
        return textureMapper.listAllTexture(state);
    }

    //更改材质的发布状态，flag为false，更改为禁止状态，disabled改为1
    @Override
    public Boolean changeTextureState(Boolean flag, Integer id) {
        //如果是更改为禁止状态，判断是一级还是二级，一级的话将二级材质全部禁止，disabled改为1
        if(textureMapper.findChildren(id).size()==0){//说明是二级材质
            textureMapper.changeTextureState(flag,id);
            return flag;
        }
        else{//说明是一级材质
           textureMapper.deleteAll(textureMapper.findChildren(id),flag);
            textureMapper.changeTextureState(flag,id);
            return flag;
        }

    }


    @CacheEvict(value = "TextureCache",allEntries = true)
    @Override
    public void clearCache() {
    }

    @Override
    public Integer insertTexture(Integer pid, String title,Integer uid) {
        Texture texture=new Texture();
        Texture texture1=new Texture();
        Userinfo userinfo=new Userinfo();
        userinfo.setId(uid);
        texture1.setId(pid);
        texture.setTexture(texture1);
        texture.setUserinfo(userinfo);
        texture.setTitle(title);
        textureMapper.insertTexture(texture);
        return texture.getId();
    }

    @Override
    public List<Texture> findAllByUserId(Integer id) {
        return textureMapper.findAllByUserId(id);
    }
}
